package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.CurrentUser;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.po.MonitorModule;
import com.zhijian.medical.entity.vo.request.MonitorModuleSearchVo;
import com.zhijian.medical.entity.vo.response.MonitorModuleVo;
import com.zhijian.medical.service.MonitorModuleService;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;

/**
 * 监测模块 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-03-12 12:40:15
 */
@Slf4j
@Controller
@Api(tags = "监测模块")
public class MonitorModuleController {

    @Autowired
    private MonitorModuleService service;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/monitor/module")
    @ApiOperation("新增")
    public boolean insert(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody MonitorModule param) {
        if (Objects.nonNull(personDto) && Objects.nonNull(personDto.getCooperativeInstitutionId())) {
            param.setFactoryId(personDto.getCooperativeInstitutionId());
        }
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != null;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/monitor/module/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/monitor/module/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody MonitorModule param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/monitor/module/{id}")
    @ApiOperation("根据id查询")
    public MonitorModuleVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/monitor/module/page")
    @ApiOperation("分页查询")
    public Page<MonitorModuleVo> selectByCondition(@RequestBody(required = false) MonitorModuleSearchVo param) {
        if (param == null)
            param = MonitorModuleSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 根据产品订单进行列表查询，不分页
     *
     * @param productId
     * @return
     */
    @Response
    @PostMapping("/monitor/module/by/product/list/{productId}")
    @ApiOperation("根据产品订单进行列表查询，不分页")
    public List<MonitorModuleVo> selectListByProductId(@ApiParam(name = "productId", value = "产品订单id")
                                                       @PathVariable("productId") Long productId) {
        return service.selectListByProductId(productId);
    }

}